package com.runing;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class MyCode {
    public static void main(String[] args) {
// 需要构建一个 代码自动生成器 对象
        AutoGenerator mpg = new AutoGenerator();
// 配置策略
// 1、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir"); //获取当前项目位置

        gc.setOutputDir(projectPath+"/src/main/java");
        gc.setAuthor("lel");
        gc.setOpen(false);
        gc.setFileOverride(true); // 是否覆盖
        gc.setServiceName("%sService"); // 去Service的I前缀
        gc.setIdType(IdType.ID_WORKER); // 主键生成策略
        gc.setDateType(DateType.ONLY_DATE); // 时间只是时间格式
        gc.setSwagger2(true); // 使用swagger2
        mpg.setGlobalConfig(gc);
//2、设置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/run?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);
//3、包的配置
        Map pathInfo = new HashMap<>();
        PackageConfig pc = new PackageConfig();
        pathInfo.put(ConstVal.ENTITY_PATH,projectPath+"/src/main/java/com/runing/entity/");
        pathInfo.put(ConstVal.CONTROLLER_PATH,projectPath+"/src/main/java/com/runing/controller/");
        pathInfo.put(ConstVal.SERVICE_PATH,projectPath+"/src/main/java/com/runing/service/");
        pathInfo.put(ConstVal.SERVICE_IMPL_PATH,projectPath+"/src/main/java/com/runing/service/impl/");
        pathInfo.put(ConstVal.MAPPER_PATH,projectPath+"/src/main/java/com/runing/mapper/");
        pathInfo.put(ConstVal.XML_PATH, projectPath+"/src/main/resources/mapper/"); // 生成xml文件路径
//        pc.setModuleName("com.runing");
//        pc.setParent("com.runing");
//        pc.setEntity("pojo");
//        pc.setMapper("dao");
//        pc.setService("service");
//        pc.setController("controller");
        pc.setParent("com.runing");
        pc.setPathInfo(pathInfo);
        mpg.setPackageInfo(pc);
//4、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("violation"); // 设置要映射的表名 多个表可设置为："admin","user","collection","comment","discuss","follow","poor","post","scenic","targets"
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true); // 自动lombok；
        strategy.setLogicDeleteFieldName("user_deleted");//逻辑删除
// 自动填充配置
        TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
        TableFill updateTime = new TableFill("update_time",FieldFill.INSERT_UPDATE); // 执行修改的时间戳
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(createTime);
        tableFills.add(updateTime);
        strategy.setTableFillList(tableFills);
// 乐观锁
//        strategy.setVersionFieldName("version");
//        strategy.setRestControllerStyle(true);
//        strategy.setControllerMappingHyphenStyle(true); //localhost:8080/hello_id_2 访问地址允许有下划线
//        mpg.setStrategy(strategy);
        mpg.setStrategy(strategy);
        mpg.execute(); //执行
    }

}
